#include "fifo_select.h"
#include <stdlib.h>  // for rand()

// Random selection strategy: Return a random index directly from 0 to numFifos-1
int selectFIFO_Random(FIFO_t fifos[], int numFifos) {
    (void)fifos;
    return rand() % numFifos;
}

// Full buffer priority policy: selects the FIFO with the largest count
int selectFIFO_FullBuffer(FIFO_t fifos[], int numFifos) {
    int selected = 0;
    int maxCount = 0;
    for (int i = 0; i < numFifos; i++) {
        if (fifos[i].count > maxCount) {
            maxCount = fifos[i].count;
            selected = i;
        }
    }
    return selected;
}

// Predictive selection strategy: The risk value is calculated according to the FIFO occupancy (count/FIFO_SIZE) and sampling frequency
// and the FIFO with the highest risk is selected
int selectFIFO_Predictive(FIFO_t fifos[], int numFifos, float sensorFreq[]) {
    int selected = 0;
    float maxRisk = 0.0f;
    for (int i = 0; i < numFifos; i++) {
        float occupancyRatio = (float)fifos[i].count / FIFO_SIZE;
        float risk = occupancyRatio * sensorFreq[i];
        if (risk > maxRisk) {
            maxRisk = risk;
            selected = i;
        }
    }
    return selected;
}
